   package com.javalong.affairs.module.userModule.fragment;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.module.userModule.activity.UserInfoActivity;
import com.javalong.affairs.base.ui.TextSelectView;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class UpdateUserInfoFragment extends BaseFragment implements
		OnClickListener {
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.et_username)
	private EditText et_username;
	@ViewInject(R.id.et_workid)
	private EditText et_workid;
	@ViewInject(R.id.et_phone)
	private EditText et_phone;
	@ViewInject(R.id.ui_tsview_dept)
	private TextSelectView tsView_dept;
	@ViewInject(R.id.bt_update)
	private Button bt_update;
	@ViewInject(R.id.tv_dept)
	private TextView tv_dept;
	private User user;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_updateuser, container,
				false);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		ViewUtils.inject(this);
		initData();
		initUI();
	}

	private void initUI() {
		bt_update.setOnClickListener(this);
		et_phone.setText(user.phone);
		et_username.setText(user.name);
		et_workid.setText(user.workId);
		tv_dept.setText(user.sectionName);
	}

	private void initData() {
		user = (User) AllData.GetInstance()
				.get(R.string.data_user_current_user);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_update:
			user.workId = et_workid.getText().toString();
			user.name = et_username.getText().toString();
			user.phone = et_phone.getText().toString();
			String url = UrlUtils.getUrl("User", "setDetail");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put(RequestKey.WORKID, et_workid.getText().toString());
			params.put(RequestKey.NAME, et_username.getText().toString());
			params.put(RequestKey.PHONE, et_phone.getText().toString());
			if (((UserInfoActivity) getActivity()).dept != null) {
				params.put(RequestKey.DEPTID,
						((UserInfoActivity) getActivity()).dept.id);
			}
			HttpUtils.post(url, params, new BaseResponseListener(
					new TypeToken<User>() {
					}.getType()) {
				@Override
				public void onSuccess(Object obj) {
					super.onSuccess(obj);
					ToastUtils.showMessage("个人信息修改成功！");
				}
			});
			break;
		default:
			break;
		}
	}
}
